package dev.entao.travel.admin

import dev.entao.base.Label
import dev.entao.core.Action
import dev.entao.core.HttpContext
import dev.entao.core.render.sendBack
import dev.entao.core.render.sendResult
import dev.entao.html.div
import dev.entao.html.linkButton
import dev.entao.log.logd
import dev.entao.page.*
import dev.entao.page.bs.tableActions
import dev.entao.sql.ConstraintException
import dev.entao.sql.EQ
import dev.entao.sql.update
import dev.entao.travel.AdminPages
import dev.entao.travel.model.Account

//@IconPair("class", "bi-person-plus")
//@SideGroup
@NavGroup(icon = "static/admin.png")
@Label("管理员账号")
class AccountPages(context: HttpContext) : AdminPages(context) {

    @Action
    fun addJson() {
        try {
            val a = Account()
            a.fromRequest()
            a.insert()

            sendResult {
                success()
            }
        } catch (ex: ConstraintException) {
            sendResult { failed("手机号已存在") }
        }
    }

    @DialogPop
    @Label("添加")
    @Action
    fun addDialog() {
        sendDialog {
            dialogTitle { +"添加账号" }
            dialogForm(::addJson) {
                div {
                    labelEdit(Account::name)
                }
                div {
                    labelEdit(Account::phone)
                }
                div {
                    labelEdit(Account::pwd)
                }
                div {
                    labelSelect(Account::status)
                }
                div {
                    labelSelect(Account::level)
                }
            }
        }
    }

    @SortParams
    @PageParams
    @NavItem(order = 0, label = "管理账号", icon = "static/admin.png")
    @Label("查询")
    @Action
    fun list() {
        val thisAction = ::list
        val w = equalProps(Account::id, Account::name, Account::phone, Account::status, Account::level)
        val rowCount = Account.count(w)
        val accountList = Account.list(w) {
            orderPage(thisAction)
            limitPage(thisAction)
        }

        for (ac in accountList) {
            ac.model["id"]?.also {
                logd("id type: ", it::class)
            }
            logd("account ID: ", ac.id)
        }


        dashboard {
            workTitle("账号查询")
            workToolbar {
                linkButton("btn", "btn-sm", "btn-outline-primary") {
                    fromAction(::addDialog)
                }
            }
            workspace {
                div("col-xxl-10", "col-xl-11", "col-lg-12") {
                    queryForm(::list) {
                        div("col-md-4", "col-sm-12") {
                            labelEdit(Account::id)
                        }
                        div("col-md-4", "col-sm-12") {
                            labelEdit(Account::name)
                        }
                        div("col-md-4", "col-sm-12") {
                            labelEdit(Account::phone)
                        }
                        div("col-md-4", "col-sm-12") {
                            labelSelect(Account::status)
                        }
                        div("col-md-4", "col-sm-12") {
                            labelSelect(Account::level)
                        }
                    }

                    tableActions(::enable, ::disable, this@AccountPages::delete)
                    tableX(accountList) {
                        itemIDProp(Account::id)
                        columnCheck()
                        columnProp(Account::id).sortable()
                        columnProp(Account::name).sortable()
                        columnProp(Account::phone).sortable()
                        columnProp(Account::pwd).sortable()
                        columnProp(Account::status).sortable()
                        columnProp(Account::level).sortable()
                        columnProp(Account::lastLogin).sortable()
                        sortAction(::list)
                        columnAction(::edit, ::del)
                    }
                    tablePaginationX(::list, rowCount)
                }
            }
        }
    }

    @ConfirmMessage("要删除这些记录吗?")
    @Label("删除")
    @Action
    fun delete(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            Account.deleteByKey(n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @Label("启用")
    @Action
    fun enable(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            Account.update(Account::status to Account.ST_ENABLE, Account::id EQ n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @Label("禁用")
    @Action
    fun disable(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            Account.update(Account::status to Account.ST_DISABLE, Account::id EQ n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @ConfirmMessage("要删除这条记录吗?")
    @Label("删除")
    @Action
    fun del(id: Int) {
        Account.deleteByKey(id)
        context.sendBack {
            removeKey("id")
            setSuccess("已删除:ID=$id 的记录")
        }
    }

    @DialogPop
    @Label("编辑")
    @Action
    fun edit(id: Int) {
        val row = Account.oneByKey(id) ?: return
        sendDialog {
            dialogTitle { +"编辑账号" }
            dialogForm(::save) {
                div {
                    labelEdit(row::id) {
                        readonly()
                    }
                }
                div {
                    labelEdit(row::name)
                }
                div {
                    labelEdit(row::phone)
                }
                div {
                    labelEdit(row::pwd)
                }
                div {
                    labelSelect(row::status)
                }
                div {
                    labelSelect(row::level)
                }
            }
        }
    }

    @Action
    fun save(id: Int) {
        try {
            val a = Account.oneByKey(id) ?: return
            a.update {
                it.fromRequest()
            }
            context.sendResult {
                success("已保存")
            }
        } catch (ex: ConstraintException) {
            context.sendResult {
                failed("名字或手机冲突")
            }
        }
    }
}